/***************************************************************************
 *            texture.c
 *
 *  Wed Oct  8 10:50:02 2008
 *  Copyright  2008  Tiago Falcao
 *  <developer@tiagofalcao.com>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
 
#include "texture.h"

//Fazer Texture Manager para so carregar uma vez a imagem

texture_manager * texture_manager_create(){
	return calloc(1,sizeof(texture_manager));
}

void texture_manager_delete(texture_manager * manager){
	uint i;
	for(i=0;i<manager->qtd;i++){
		//texture_image_delete(manager->texs[i].image,manager->texs[i].w,manager->texs[i].h);
		free(manager->texs[i].file);
	}
	free(manager->texs);
	free(manager);
}

GLuint texture_manager_get(texture_manager * manager, char * file){
	uint i = 0;
	while(i<manager->qtd){
		if(!strcmp(manager->texs[i].file,file))
			return manager->texs[i].id;
		i++;
	}
	return texture_manager_add(manager,file);
}

GLuint texture_manager_add(texture_manager * manager, char * file){
	uint i = manager->qtd;
	manager->texs=realloc(manager->texs,(++manager->qtd)*sizeof(texture));
	manager->texs[i].file=malloc((strlen(file)+1)*sizeof(char));
	strcpy(manager->texs[i].file,file);
	printf("Texture file=%s\n",manager->texs[i].file);
	manager->texs[i].id== SOIL_load_OGL_texture
	(
		file,
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);
	printf("Texture=%d\n",manager->texs[i].id);
	//manager->texs[i].w=w;
	//manager->texs[i].h=h;
	//manager->texs[i].image=image;
	//glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA, w, h, 0,  GL_RGBA, GL_UNSIGNED_BYTE, manager->texs[i].image); 
	return manager->texs[i].id;
}

/*GLubyte*** texture_image_create(uint w,uint h){
	GLubyte*** image;
	GLubyte** iline;
	uint i,j;
	uint ws=w*sizeof(GLubyte**);
	uint cs=sizeof(GLubyte**)<<2;
	image=malloc(h*sizeof(GLubyte**));
	for(i=0;i<h;i++){
		image[i]=iline=malloc(ws);
		for(j=0;j<w;j++)
			iline[j]=malloc(cs);
	}
	return image;
}

void texture_image_delete(GLubyte*** image,uint w,uint h){
	GLubyte** iline;
	uint i,j;
	for(i=0;i<h;i++){
		iline=image[i];
		for(j=0;j<w;j++)
			free(iline[j]);
		free(iline);
	}
	free(image);
}*/
	
